using Gtk;
using System;

namespace Serpis
{
	public class ValidatorViewAttribute : PluginAttribute
	{
		private ListStore listStore;
		private Expander expander;
		public override Controller Controller {
			set {
				base.Controller = value;

				Container container = (Gtk.Container)((Gtk.Bin)controller.View).Child;
				expander = new Expander("Detalles");
				expander.Visible = false;
				ScrolledWindow scrolledWindow = new ScrolledWindow();
				scrolledWindow.Visible = true;
				TreeView treeView = new TreeView();
				treeView.Visible = true;
				scrolledWindow.Add(treeView);
				expander.Add(scrolledWindow);
				container.Add(expander);
				
				treeView.AppendColumn("Propiedad", new CellRendererText(), "text", 0);
				treeView.AppendColumn("Error", new CellRendererText(), "text", 1);
				listStore = new ListStore(typeof(string), typeof(string));
				treeView.Model = listStore;
			}
		}
		
		public override void Refresh ()
		{
			listStore.Clear();
			PropertyError[] propertyErrors = controller.Validator.Errors;
			foreach (PropertyError propertyError in propertyErrors)
				listStore.AppendValues(propertyError.Property, propertyError.Error);
			
			expander.Visible = propertyErrors.Length > 0;
		}
	}
}
